import {takeEvery,put} from 'redux-saga/effects'
import {addProductToCart,addroductToLocalCart,loadCarts,saveCarts,deleteProductFromCart,deleteProductFromLocalCart,changeServiceProductNumber,changeLocalProductNumber} from '../actions/cart.action'
import axios from 'axios'

function* handleAddProductToCart(action) {
  const {data} = yield axios.post('http://localhost:3005/cart/add',{gid:action.payload})
  // 已经添加到购物车的数据
  yield put(addroductToLocalCart(data))
}
function* handleLoadCarts(action){
  const {data} = yield axios.get('http://localhost:3005/cart')
  // 已经添加到购物车的数据
  yield put(saveCarts(data))
}
function* handleDeleteProductFromCart(action){
  const {data} = yield axios.delete('http://localhost:3005/cart/delete',{
    params: {
      cid: action.payload
    }
  })
  // 已经添加到购物车的数据
  yield put(deleteProductFromLocalCart(data.index))
}

function* handleChangeServiceProductNumber(action){
  const {data} = yield axios.put('http://localhost:3005/cart',action.payload)
  yield put(changeLocalProductNumber(data))
}
export default function* cartSaga(){
  yield takeEvery(addProductToCart,handleAddProductToCart)
  yield takeEvery(loadCarts,handleLoadCarts)
  yield takeEvery(deleteProductFromCart,handleDeleteProductFromCart)
  yield takeEvery(changeServiceProductNumber,handleChangeServiceProductNumber)
}